﻿// Author:					HungNM
// Created:					05-08-2012
// Last Modified:			05-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class CitiesManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindCities();
        }


        private void PopulateLabels()
        {
            Title = "Quản trị thành phố - eProject";
        }

        protected void grvCities_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                grvCities.EditIndex = e.NewEditIndex;
                BindCities();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvCities_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("CancelInsert"))
            {
                grvCities.ShowFooter = false;
                BindCities();
            }
            if (e.CommandName.Equals("EmptyInsert"))
            {
                GridViewRow row = (GridViewRow)((e.CommandSource as ImageButton).NamingContainer);
                TextBox cityName = row.FindControl("txtName") as TextBox;
                Cities city = new Cities();
                city.Name = cityName.Text.Trim();
                if (city.Save())
                {
                    grvCities.ShowFooter = false;
                    BindCities();
                }

            }
            if (e.CommandName.Equals("Insert"))
            {
                TextBox cityName = grvCities.FooterRow.FindControl("txtName") as TextBox;
                Cities city = new Cities();
                city.Name = cityName.Text.Trim();
                if (city.Save())
                {
                    grvCities.ShowFooter = false;
                    BindCities();
                }

            }
        }

        protected void grvCities_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int id = int.Parse(grvCities.DataKeys[e.RowIndex].Value.ToString());
                Cities city = new Cities(id);
                city.Name = ((TextBox)grvCities.Rows[e.RowIndex].FindControl("txtName")).Text;
                city.Save();
                grvCities.EditIndex = -1;
                BindCities();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvCities_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                grvCities.EditIndex = -1;
                BindCities();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvCities_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //try
            //{
            //    int id = Convert.ToInt32(grvCities.DataKeys[e.RowIndex].Value.ToString());
            //    if (Cities.Delete(id))
            //        BindCities();
            //}
            //catch (System.Exception ex)
            //{
            //    throw ex;
            //}
        }

        protected void grvCities_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCities.PageIndex = e.NewPageIndex;
            grvCities.DataBind();
            BindCities();
        }

        private void LoadSettings()
        {

        }

        private void LoadParams()
        {

        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnAddNew.Click += new EventHandler(btnAddNew_Click);
            grvCities.RowCommand += new GridViewCommandEventHandler(grvCities_RowCommand);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            grvCities.ShowFooter = true;
            BindCities();
        }

        private void BindCities()
        {
            try
            {
                grvCities.DataSource = Cities.GetAll();
                grvCities.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}